<template>
  <el-dialog
    title="充值缴费"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="600px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="房间信息" prop="roomInfo">
        <el-input v-model="dataForm.roomInfo" disabled></el-input>
      </el-form-item>
      <el-form-item label="业主信息" prop="ownerInfo">
        <el-input v-model="dataForm.ownerInfo" disabled></el-input>
      </el-form-item>
      <el-form-item label="当前余额" prop="currentBalance">
        <el-input v-model="dataForm.currentBalance" disabled>
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="充值金额" prop="rechargeAmount">
        <el-input v-model="dataForm.rechargeAmount" placeholder="请输入充值金额">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-select v-model="dataForm.paymentMethod" placeholder="请选择支付方式">
          <el-option label="现金" value="cash"></el-option>
          <el-option label="微信" value="wechat"></el-option>
          <el-option label="支付宝" value="alipay"></el-option>
          <el-option label="银行卡" value="bank"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定充值</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        roomInfo: '',
        ownerInfo: '',
        currentBalance: '',
        rechargeAmount: '',
        paymentMethod: '',
        remark: ''
      },
      dataRule: {
        rechargeAmount: [
          { required: true, message: '充值金额不能为空', trigger: 'blur' },
          { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的金额格式', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(row) {
      this.dataForm = {
        id: row.id,
        roomInfo: row.roomInfo,
        ownerInfo: row.ownerInfo,
        currentBalance: row.amount,
        rechargeAmount: '',
        paymentMethod: '',
        remark: ''
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(async (valid) => {
        if (valid) {
          try {
            await this.$api.finance_recharge_save(this.dataForm)
            this.$message({
              message: '充值成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } catch (error) {
            this.$message.error('充值失败')
          }
        }
      })
    }
  }
}
</script>

